document.addEventListener('DOMContentLoaded', function() {
    const loginForm = document.getElementById('loginForm');
    
    loginForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        
        // 发送登录请求
        fetch(`http://localhost:8080/auth/login/teacher?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.code === 200) {
                // 登录成功，存储用户信息
                sessionStorage.setItem('isLoggedIn', 'true');
                sessionStorage.setItem('username', data.data.username);
                sessionStorage.setItem('teacherId', data.data.teacherId);
                // 跳转到主页面
                window.location.href = 'dashboard.html';
            } else {
                alert(data.message || '登录失败！');
            }
        })
        .catch(error => {
            alert('网络错误，请稍后重试！');
            console.error('Error:', error);
        });
    });
}); 